/**
 * 
 */
var xmlhttp;
function initAjax() {//初始化ajax
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
//提交订单
function commitOrders() {
	var table=document.getElementById("books");
	var data="orderList=[";
	for(var i=1;i<table.rows.length-1;i++){
		if(table.rows[i].cells[0].getElementsByTagName("input")[0].checked){
			var bookISBN=table.rows[i].cells[0].getElementsByTagName("input")[0].value;
			data+="{ISBN:"+bookISBN+"},";
		}
	}
	data+="]";
	var url="../OrderServlet?opt=commit";
	initAjax();
	xmlhttp.open("post", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function (){
		//Ajax引擎状态为成功  
	    if (xmlhttp.readyState == 4) {
	        //HTTP协议状态为成功  
	        if (xmlhttp.status == 200) {
	        	var data=eval(xmlhttp.responseText);
	        	if(data[0].login=="nologin"){
	        		alert("尚未登陆");
	        		window.location.href="login.jsp";
	        	}else{
	        		if(data[0].commit=="committrue"){
	        			alert("订单提交成功");
	        			window.location.href="orders.jsp";
	        		}else{
	        			alert("订单提交失败");
	        		}
	        	}
	        }
	    }
	};
	xmlhttp.send(data);
}
//查询用户订单
function userAllOrderList() {
	var url="../OrderServlet?opt=showOrders";
	var data="data=";
	initAjax();
	xmlhttp.open("post",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function (){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				var data=eval(xmlhttp.responseText);
				if(document.getElementById("ordersTitle")){
					var tr=document.getElementById("ordersTitle");
					tr.parentNode.removeChild(tr);
				}
				if(document.getElementById("myOrdersList")){
					var t=document.getElementById("myOrdersList");
					t.parentNode.removeChild(t);
				}
				if(document.getElementById("mess")){
					var m=document.getElementById("mess");
					m.parentNode.removeChild(m);
				}
				if(data!==null&&data!=""){
					if(data[0].login=="nologin"){
						alert("尚未登陆");
		        		window.location.href="login.jsp";
					}else{
						var orders=document.getElementById("orders");
						var tr_1=document.createElement("ul");
						tr_1.setAttribute("id", "ordersTitle");
						tr_1.setAttribute("class", "ordersTitle");
						orders.appendChild(tr_1);
						var array=new Array("one","two","three","four","five","six","订单信息","收获人","订单金额","订单时间","当前状态","操作");
						for(var y=0;y<array.length/2;y++){
							var th_1=document.createElement("li");
							th_1.setAttribute("class", array[y]);
							th_1.innerHTML=array[y+array.length/2];
							tr_1.appendChild(th_1);
						}
						var table=document.createElement("table");
						table.setAttribute("id", "myOrdersList");
						orders.appendChild(table);
						
						for(var i=0;i<data.length;i++){
							var tr=document.createElement("tr");
							table.appendChild(tr);
							var th=document.createElement("th");
							tr.appendChild(th);
							var t=document.createElement("table");
							t.setAttribute("class", "bookList");
							th.appendChild(t);
							
							var tr_2=document.createElement("tr");
							tr_2.setAttribute("class", "orderInfo");
							t.appendChild(tr_2);
							array=new Array("one","two","three","four","five","six","订单号："+data[i].id,data[i].user.userName,data[i].totalPrice,(data[i].orderDate).replace(".0",""),"完成","");
							for(var y=0;y<array.length/2;y++){
								var th_2=document.createElement("th");
								th_2.setAttribute("class", array[y]);
								th_2.innerHTML=array[y+array.length/2];
								tr_2.appendChild(th_2);
								if(y==5){
									var a_delete=document.createElement("a");
									a_delete.setAttribute("href","#");
									a_delete.setAttribute("onclick","deleteOrder("+data[i].id+")");
									a_delete.innerHTML="删除";
									th_2.appendChild(a_delete);
								}
							}
							for(var j=0;j<data[i].orderBooks.length;j++){
								var tr_3=document.createElement("tr");
								tr_3.setAttribute("class", "bookInfo");
								t.appendChild(tr_3);
								var th_3_1=document.createElement("th");
								th_3_1.setAttribute("class", "one");
								tr_3.appendChild(th_3_1);
								var a_1=document.createElement("a");
								a_1.setAttribute("href", "bookInfo.jsp?book="+data[i].orderBooks[j].book.id);
								a_1.setAttribute("class", "bookImg");
								th_3_1.appendChild(a_1);
								var img=document.createElement("img");
								img.setAttribute("src", "../style/img/book_logo/"+data[i].orderBooks[j].book.picPath);
								a_1.appendChild(img);
								var a_2=document.createElement("a");
								a_2.setAttribute("href", "bookInfo.jsp?book="+data[i].orderBooks[j].book.id);
								a_2.setAttribute("class", "bookName");
								a_2.innerHTML=data[i].orderBooks[j].book.title;
								th_3_1.appendChild(a_2);
								
								array=new Array("two","three","four","five","six","",data[i].orderBooks[j].book.unprice+"x"+data[i].orderBooks[j].quantity,"","","");
								for(var y=0;y<array.length/2;y++){
									var th_3=document.createElement("th");
									th_3.setAttribute("class", array[y]);
									th_3.innerHTML=array[y+array.length/2];
									tr_3.appendChild(th_3);
								}
							}
						}
					}
				}else{
					var orders=document.getElementById("orders");
					var mess=document.createElement("div");
					mess.setAttribute("class", "mess");
					mess.innerHTML="无任何订单信息";
					orders.appendChild(mess);
				}
			}
		}
	};
	xmlhttp.send(data);
}
function deleteOrder(orderId){
	var url="../OrderServlet?opt=deleteOrders";
	var data="data={'orderId':'"+orderId+"'}";
	initAjax();
	xmlhttp.open("post",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange=function (){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				var data=eval(xmlhttp.responseText);
				if(data!=""&&data!==null){
					if(data[0].deleteOrder=="success"){
						alert("订单删除成功");
						userAllOrderList();
					}else{
						alert("删除失败");
					}
				}
			}
		}
	};
	xmlhttp.send(data);
}